package com.xpfirst.socket;

import java.io.IOException;
import java.net.Socket;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.xpfirst.ActivityManage;
import com.xpfirst.socket.SocketClient;

public class SocketManage {
	public static SocketClient socketClient = null;
	public static Thread socketThread;
	public static void init(String host){
		if(socketClient != null){
			socketClient.close();
			socketClient = null;
		}
		socketClient = new SocketClient();
		socketClient.init(host,54321);
		socketThread=new Thread(socketClient);
		socketThread.start();
	}
	public static void write(String str){
		if(socketClient != null){
			socketClient.write(str);
		}
	}
	public static void close(){
        ActivityManage.sendMsg(0, "socket", "close");
		if(socketClient != null){
			socketClient.close();
			socketClient = null;
		}
		if(socketThread != null){
			if(socketThread.isAlive()){
				socketThread.stop();
			}
			socketThread = null;
		}
	}
}
